package com.ludaxun.mapapi.controller;


import com.alibaba.fastjson.JSON;
import com.ludaxun.mapapi.utils.R;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/trackForNumber")
public class TrackForNumberController {





    @PostMapping("/getTreack")
    public R findTrackByNumber() throws UnirestException {


        HttpResponse<String> response1 = Unirest.post("http://prevideo.hywtech.com/ocm/gps/listCar")
                .header("token", "3821691749632000")
                .asString();

        Map stu1 = JSON.parseObject(response1.getBody(), Map.class);

        List<String> numberS=new LinkedList<>();

        List<Map> numberS1=(List)stu1.get("data");

        numberS1.forEach(item->{
            numberS.add((String)item.get("carnumber"));
        });




        List<Map> re=new LinkedList<>();

        Unirest.setTimeouts(0, 0);
        numberS.forEach(item->{

            try {

                HttpResponse<String> response = Unirest.post("http://prevideo.hywtech.com/ocm/gps/getLastPosition")
                        .header("token", "3821691749632000")
                        .field("carnumber", item)
                        .asString();

                Map stu = JSON.parseObject(response.getBody(), Map.class);
                re.add(stu);
//                re.add(response.getBody());

            } catch (UnirestException e) {
                throw new RuntimeException(e);
            }

        });

        return R.ok(re);
    }



}
